<!--Descriptive page name, messages and instructions-->
<h2><%= ts('Wrangle Tags for') %> <%= link_to_tag(@tag) %></h2>
<!--/descriptions-->
<!--subnav-->
<ul class="navigation">
<li><%= link_to 'edit tag and parent associations', {:controller => :tags, :action => :edit, :id => @tag} %></li>
<li><%= tag_comment_link(@tag) %></li>
</ul>
<!--/subnav-->

<!--main content-->
<%
  showing_header = ""
  if params[:show] && params[:status]
    showing_header = "Showing " + params[:status] + " " + params[:show]
  elsif params[:show]
    showing_header = "Showing " + params[:show]
  end
%>
<% unless showing_header.blank? %>
<h3><%= showing_header %></h3>
<% end %>

<% if @tag.is_a?(Fandom) && params[:show] == "relationships" %>
<p><%= link_to "wrangle relationships to characters", tag_wranglings_path(:show => 'character_relationships', :fandom_string => @tag.name) %></p>
<% end %>

<% if params[:show] && params[:show] !~ /suggested/ %>
  <h3>Add new <%= params[:show] %></h3>
  <%= form_for :tag, @tag, :url => { :action => "update", :id => @tag}, :html => { :method => :put } do |f| %>
    <%= f.text_area "#{params[:show].singularize}_string", :size => "100x5" %>
    <%= autocomplete_text_field_with_type @tag, "tag_#{params[:show].singularize}_string" %>
    <%= hidden_field_tag :show, params[:show] %>
    <%= hidden_field_tag :sort_column, params[:sort_column] %>
    <%= hidden_field_tag :sort_direction, params[:sort_direction] %>
    <%= hidden_field_tag :page, params[:page] %>
    <%= hidden_field_tag :status, params[:status] %>
    <%= f.submit 'Wrangle' %>
  <% end %>
<% end %>

<% if params[:show] %>
  <ul class="navigation">
    <li>Show: </li>
    <li><%= link_to 'all', url_for(:show => params[:show], :status => 'all') %></li>
    <li><%= link_to 'canonical', url_for(:show => params[:show], :status => 'canonical') %></li>
    <li><%= link_to 'non-canonical', url_for(:show => params[:show], :status => 'noncanonical') %></li>
    <li><%= link_to 'unfilterable', url_for(:show => params[:show], :status => 'unfilterable'), :title => "non-canonical tags without synonyms" %></li>
    <li><%= link_to 'unwrangled', url_for(:show => params[:show], :status => 'unwrangled'), :title => "tags which were used on the same works but haven't been wrangled yet" %></li>
  </ul>
<% end %>

<% if @tags && @tags.empty? %>
  <p>There are no tags in this category at the moment.</p>
<% elsif @tags %>
  <%= will_paginate @tags %>

  <%= form_tag url_for(:controller => 'tags', :action => 'mass_update'), :method => :post, :id => 'wrangulator' do %>
    <p class="submit"><%= submit_tag "Wrangle" %></p>
    <% sort_url_options = {:show => params[:show], :status => params[:status], :sort_column => params[:sort_column]} %>
    <table>
      <tr>
        <th>
          <%= sort_link t('.sort.tag_name', :default => 'Tag Name'), :name, {:sort_default => true} %>
        </th>
        <% if params[:show] == "relationships" %>
        <th>Characters</th>
        <% end %>
        <th>Canonical</th>
        <% if params[:status] == "canonical" %>
        <th>Metatag</th>
        <% else %>
        <th>Synonym</th>
        <% end %>
        <th>
          <%= sort_link t('.sort.created_at', :default => 'Created'), :created_at, {:desc_default => true} %>
        </th>
        <th>
          <%= sort_link t('.sort.taggings_count', :default => 'Taggings Count'), :taggings_count, {:desc_default => true} %>
        </th>
        <th scope="col">Manage</th>
      </tr>
      <% for tag in @tags %>
      <tr>
        <td>
          <%= label_tag "canonicals_#{tag.id}", tag.name %>
        </td>

        <% if params[:show] == "relationships" %>
        <td>
          <% unless !tag.canonical? || tag.characters.empty? %>
          <ul><%= tag_link_list(tag.characters) %></ul>
          <% end %>
        </td>
        <% end %>
        <td>
          <% if tag.canonical? %>
          Yes
          <% else %>
          <%= check_box_tag "canonicals[]", tag.id, tag.canonical?, :id => "canonicals_#{tag.id}" %>
          <% end %>
        </td>
        <td>
        <% if params[:status] == "canonical" %>
          <% unless tag.direct_meta_tags.blank? %><%= tag_link_list(tag.direct_meta_tags) %><% end %>
        <% else %>
          <% if tag.merger %><%= link_to_tag(tag.merger) %><% end %>
        <% end %>
        </td>
        <td><%= tag.created_at.to_date %></td>
        <td><%= tag.taggings_count %></td>
        <td class="navigation">
          <ul class="navigation">
            <% unless params[:status] == "unwrangled" %>
            <li><%= link_to 'remove', {:controller => :tags, :action => :remove_association, :id => @tag, :to_remove => tag.name} %></li>
            <% end %>
            <li><%= link_to 'edit', {:controller => :tags, :action => :edit, :id => tag} %></li>
            <li><%= link_to 'wrangle', {:controller => :tags, :action => :wrangle, :id => tag} %></li>
            <li><%= link_to 'works', {:controller => :works, :action => :index, :tag_id => tag} %></li>
          </ul>
        </td>
      </tr>
      <% end %>
    </table>
    <p>
    <%= hidden_field_tag :show, params[:show] %>
    <%= hidden_field_tag :sort_column, params[:sort_column] %>
    <%= hidden_field_tag :sort_direction, params[:sort_direction] %>
    <%= hidden_field_tag :page, params[:page] %>
    <%= hidden_field_tag :status, params[:status] %>
    </p>
    <p class="submit"><%= submit_tag "Wrangle" %></p>
  <% end %>
<!--/content-->
<!--subnav-->

  <%= will_paginate @tags %>
<% end %>
<!--/subnav-->
